package com.example.project.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


public class DateUtils {

    private static final SimpleDateFormat defaultFormat=new SimpleDateFormat("yyyy-MM-dd");

    public static Date stringToDate(String dateString,String pattern,String message){
        try {
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat(pattern);
            simpleDateFormat.setLenient(false);
            return simpleDateFormat.parse(dateString);
        } catch (ParseException e) {
            throw new IllegalArgumentException(message);
        }
    }


    public static Date stringToDate(String dateString,String pattern){
        try {
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat(pattern);
            simpleDateFormat.setLenient(false);
            return simpleDateFormat.parse(dateString);
        } catch (ParseException e) {
            return null;
        }
    }


    public static long stringToTime(String dateString,String pattern){
        try {
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat(pattern);
            simpleDateFormat.setLenient(false);
            Date data=simpleDateFormat.parse(dateString);
            return data.getTime();
        } catch (ParseException e) {
            throw new IllegalArgumentException(e.getMessage());
        }
    }

    public static String dateToString(Date date) {
        return defaultFormat.format(date);
    }

    public static String dateToString(Date date,String pattern) {
        return new SimpleDateFormat(pattern).format(date);
    }



    public static Date getTimeStrToDate(String time,String pattern){
        try{
            long timeL=Long.valueOf(time);
            Date date=new Date(timeL);
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat(pattern);
            String str=simpleDateFormat.format(date);
            date=simpleDateFormat.parse(str);
            return date;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public static String timeStrToString(String timeStr,String pattern) {
        Date date = new Date(Long.valueOf(timeStr));
        return new SimpleDateFormat(pattern).format(date);
    }

    public static String timeStrToString(long timeStr,String pattern) {
        Date date = new Date(timeStr);
        return new SimpleDateFormat(pattern).format(date);
    }

    public static Long timeToTime(long timeL,String pattern) {
        try{
            Date date=new Date(timeL);
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat(pattern);
            String str=simpleDateFormat.format(date);
            date=simpleDateFormat.parse(str);
            return date.getTime();
        }catch (Exception e){
            e.printStackTrace();
            return 0L;
        }
    }


    /**
     *
     * @param dateString 时间字符串
     * @param returnType
     * @return
     */
    public  static long dateInterval(String dateString,String pattern ,long returnType){
        long  interval = 0;
        try {
            Date startdate  =  stringToDate(dateString,pattern);
            long startMills = startdate.getTime();
            long  nowMills = System.currentTimeMillis();
            interval = (nowMills-startMills)/(returnType*1000);
        }catch (Exception e){

        }
        return interval;
    }

    public static Date addDays(Date paramDate, int paramInt)
            throws Exception {
        Calendar localCalendar = Calendar.getInstance();
        localCalendar.setTime(paramDate);
        int i = localCalendar.get(Calendar.DAY_OF_YEAR);
        localCalendar.set(Calendar.DAY_OF_YEAR, i + paramInt);
        return localCalendar.getTime();
    }


    public static Date addYears(Date paramDate, int paramInt)
            throws Exception {
        Calendar localCalendar = Calendar.getInstance();
        localCalendar.setTime(paramDate);
        int i = localCalendar.get(Calendar.YEAR);
        localCalendar.set(Calendar.YEAR, i + paramInt);
        return localCalendar.getTime();
    }
    /**
     *  日期字符串  加天数
     * @param strDate 字符串日期
     * @param paramInt  加多少天
     * @return
     * @throws Exception
     */
    public static String addStringDays(String strDate, int paramInt){
        String returnStr=strDate;
        try {
            Date date = DateUtils.stringToDate(strDate,"yyyy-MM-dd");
            Calendar   calendar   =   new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(calendar.DATE,paramInt);//把日期往后增加一天.整数往后推,负数往前移动
            returnStr=DateUtils.dateToString(calendar.getTime());
        }catch (Exception e){


        }
        return  returnStr;
    }

    /**
     *  日期字符串  加天数
     * @param time 时间戳
     * @param paramInt  加多少年
     * @return
     * @throws Exception
     */
    public static long addStringYearsRT(long time, int paramInt){
       long returnTime=-1;
        try {
            Date date = new Date(time);
            Calendar   calendar   =   new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(Calendar.YEAR,paramInt);//把日期往后增加N年.整数往后推,负数往前移动
            returnTime=calendar.getTimeInMillis();
        }catch (Exception e){
        }
        return  returnTime;
    }

    /**
     *  日期字符串  加天数
     * @param time 时间戳
     * @param paramInt  加多少年
     * @return
     * @throws Exception
     */
    public static long addStringMonthRT(long time, int paramInt){
        long returnTime=-1;
        try {
            Date date = new Date(time);
            Calendar   calendar   =   new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH,paramInt);//把日期往后增加N年.整数往后推,负数往前移动
            returnTime=calendar.getTimeInMillis();
        }catch (Exception e){
        }
        return  returnTime;
    }

    /**
     *  日期字符串  加天数
     * @param time 时间戳
     * @param paramInt  加多少年
     * @return
     * @throws Exception
     */
    public static long addStringDayRT(long time, int paramInt){
        long returnTime=-1;
        try {
            Date date = new Date(time+(paramInt*24*60*60*1000));
            returnTime=date.getTime();
        }catch (Exception e){
        }
        return  returnTime;
    }

    /**
     *  日期字符串  加天数
     * @param time 时间戳
     * @param paramInt  加多少年
     * @return
     * @throws Exception
     */
    public static long addStringDayRT(long time, long paramInt){
        long returnTime=-1;
        try {
            Date date = new Date(time+(paramInt*24*60*60*1000));
            returnTime=date.getTime();
        }catch (Exception e){
        }
        return  returnTime;
    }

    /**
     *  日期字符串  减天数
     * @param time 时间戳
     * @param paramInt  加多少年
     * @return
     * @throws Exception
     */
    public static long minusStringDayRT(long time, long paramInt){
        long returnTime=-1;
        try {
            Date date = new Date(time-(paramInt*24*60*60*1000));
            returnTime=date.getTime();
        }catch (Exception e){
        }
        return  returnTime;
    }



    /**
     *  返回当天时间戳的０点
     * @param time 时间戳
     * @return
     * @throws Exception
     */
    public static long timeToTimeLow(long time){
        long returnTime=-1;
        try {
            Date date = new Date(time);
            Calendar   calendar   =   new GregorianCalendar();
            calendar.setTime(date);
            calendar.set(Calendar.HOUR_OF_DAY,0);
            calendar.set(Calendar.MINUTE,0);
            calendar.set(Calendar.SECOND,0);
            returnTime=calendar.getTimeInMillis();
        }catch (Exception e){
        }
        return  returnTime;
    }


    /**
     *  返回当天时间戳的23:59:59点
     * @param time 时间戳
     * @return
     * @throws Exception
     */
    public static long timeToTimeTop(long time){
        long returnTime=-1;
        try {
            Date date = new Date(time);
            Calendar   calendar   =   new GregorianCalendar();
            calendar.setTime(date);
            calendar.set(Calendar.HOUR_OF_DAY,23);
            calendar.set(Calendar.MINUTE,59);
            calendar.set(Calendar.SECOND,59);
            returnTime=calendar.getTimeInMillis();
        }catch (Exception e){
        }
        return  returnTime;
    }


    /**
     *  获取当前年月
     * @return
     * @throws Exception
     */
    public static String  getCurrentYearMonth(){
        String returnTime="";
        try {
          returnTime = new SimpleDateFormat("yyyyMM").format(new Date());
        }catch (Exception e){
        }
        return  returnTime;
    }

    /**
     *  获取上月年月
     * @return
     * @throws Exception
     */
    public static String  getLastYearMonth(){
        String returnTime="";
        try {
            Calendar c = Calendar.getInstance();
            c.add(Calendar.MONTH, -1);

            returnTime = new SimpleDateFormat("yyyyMM").format(c.getTime());
        }catch (Exception e){
        }
        return  returnTime;
    }


    /**
     *  获取当前年月的上月　-1,上上月　-2
     * @return
     * @throws Exception
     */
    public static String  getYearMonth(int i){
        String returnTime="";
        try {
            Calendar c = Calendar.getInstance();
            c.add(Calendar.MONTH, i);
            returnTime = new SimpleDateFormat("yyyyMM").format(c.getTime());
        }catch (Exception e){
        }
        return  returnTime;
    }


    /**
     *  获取当前日期的时间戳
     * @return
     * @throws Exception
     */
    public static Long  getCurrentDateToTime(){
        Long returnTime=0L;
        try {
            Date date=new Date();
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            String str=simpleDateFormat.format(date);
            date=simpleDateFormat.parse(str);
            return date.getTime();
        }catch (Exception e){
        }
        return  returnTime;
    }


    /**
     *  获取当前时间的时间戳
     * @return
     * @throws Exception
     */
    public static Long  getCurrentTime(){
        return  System.currentTimeMillis();
    }

    /**
     * 当前季度的开始年月
     * @return
     */
    public static String getQuarterStartYearMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if(currentMonth >= 1 && currentMonth <= 3) {
                c.set(Calendar.MONTH, 0);
            }else if (currentMonth >= 4 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 3);
            }else if (currentMonth >= 7 && currentMonth <= 9) {
                c.set(Calendar.MONTH, 6);
            }else if (currentMonth >= 10 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 9);
            }
            c.set(Calendar.DATE, 1);
            return new SimpleDateFormat("yyyyMM").format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *  获取业绩日期
     * @param  yearMonth 业绩年月　yyyyMM
     * @return
     * @throws Exception
     */
    public static Long  getBusinessDate(String yearMonth,String bizDay){
        try {
            String day  = yearMonth+bizDay;
            return DateUtils.stringToTime(day,"yyyyMMdd");
        }catch (Exception e){
        }
        return  0L;
    }

}
